//
//  BCRechargeCoinController.m
//  GSBlockChain
//
//  Created by guoshikeji_a on 2018/11/15.
//  Copyright © 2018年 guoshikeji. All rights reserved.
//

#import "BCRechargeCoinController.h"


//选择币种
#import "BChooseCoinTypeController.h"
//按钮视图
#import "BCWalletBottomView.h"
//选择币种
#import "BCRechargeCoinTypeButton.h"
//二维码的视图
#import "BCCoinTypeQrCodeView.h"

#import "BCSavePhotoManager.h"

//request
#import "BCUserWalletCurrRequest.h"
#import "UserWalletChargeRequest.h"//界面数据

@interface BCRechargeCoinController () <BChooseCoinTypeControllerDelegate,BCCoinTypeQrCodeViewDelegate>

/**
 *BCWalletBottomView
 */
@property (nonatomic, strong) BCWalletBottomView *bottomView;

/**
 *选择币种按钮
 */
@property (nonatomic, strong) BCRechargeCoinTypeButton *coinTypeButton;

@property (nonatomic, strong) BCCoinTypeQrCodeView *codeView;



/**
 *币种列表数据
 */
@property (nonatomic, strong) BCUserWalletCurrData *walletCurrData;

@end

@implementation BCRechargeCoinController

- (void)viewDidLoad {
    [super viewDidLoad];
    //
//    self.title = BCLocalizedKey(@"wallet_recharge_coin");
    //
    [self setupSubviews];
    //
    [self requestChargeCurrId:nil init:true change:false];
    //默认请求一次
    [self requestCurrList];
}
- (void)setupSubviews {
    self.coinTypeButton = [BCRechargeCoinTypeButton new];
    [self.view addSubview:self.coinTypeButton];
    [self.coinTypeButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.customNaviBar.mas_bottom).offset(20.0);
        make.centerX.mas_equalTo(self.view);
        make.width.mas_equalTo(self.view).offset(-40.0);
        make.height.mas_equalTo(41.0);
    }];
    [self.coinTypeButton addTarget:self action:@selector(coinTypeButtonDidClickedAction) forControlEvents:UIControlEventTouchUpInside];
    //
    self.bottomView = [BCWalletBottomView new];
    [self.view addSubview:self.bottomView];
    [self.bottomView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.bottom.right.mas_equalTo(self.view);
        make.height.mas_equalTo([BCWalletBottomView defaultHeight]);
    }];
//    [self.bottomView.leftButton.displayButton setTitle:BCLocalizedKey(@"wallete_copy_address") forState:UIControlStateNormal];
    [self.bottomView.leftButton addTarget:self action:@selector(addressButtonDidClickedAction) forControlEvents:UIControlEventTouchUpInside];
//    [self.bottomView.rightButton.displayButton setTitle:BCLocalizedKey(@"wallete_save_qr") forState:UIControlStateNormal];
    [self.bottomView.rightButton addTarget:self action:@selector(saveButtonDidClickedAction) forControlEvents:UIControlEventTouchUpInside];
    self.bottomView.rightButton.adjustsButtonWhenDisabled = true;
    self.bottomView.leftButton.adjustsButtonWhenDisabled = true;
    
    self.codeView = [BCCoinTypeQrCodeView new];
    self.codeView.codeDelegate = self;
    [self.view addSubview:self.codeView];
    [self.codeView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.coinTypeButton.mas_bottom).offset(20.0);
        make.left.right.mas_equalTo(self.view);
        make.bottom.mas_equalTo(self.bottomView.mas_top);
    }];
    [self.view layoutIfNeeded];
    self.coinTypeButton.alpha = 0;
    self.bottomView.alpha = 0;
    self.codeView.alpha = 0;
}




#pragma mark - method buttonDidClickedAction
//币种选择
- (void)coinTypeButtonDidClickedAction {
    BChooseCoinTypeController *coinTypeVC = [BChooseCoinTypeController new];
    coinTypeVC.walletCurrData = self.walletCurrData;
    coinTypeVC.delegate = self;
    [self.navigationController presentViewController:coinTypeVC animated:true completion:nil];
}

#pragma mark - BChooseCoinTypeControllerDelegate
- (void)chooseCoinTypeControllerDidRequestCurrDataSuccess:(BCUserWalletCurrData *)currData {
    self.walletCurrData = currData;
}
- (void)chooseCoinTypeControllerDidSelectCurrDataModel:(BCUserWalletCurrDataModel *)dataModel {
    self.coinTypeButton.currData = dataModel;
    self.coinTypeButton.coinType = dataModel.acronyms;
    NSString *currId = [NSString stringWithFormat:@"%@",@(dataModel.cur_id)];
    [self requestChargeCurrId:currId init:false change:true];
}


#pragma mark -
- (void)requestChargeCurrId:(NSString *)currId init:(BOOL)init change:(BOOL)change {
    if (init) {
        [self.view bc_showLoadingInsets:UIEdgeInsetsMake(CGRectGetHeight(self.customNaviBar.frame), 0, 0, 0)];
    }
    if (change) {
        self.bottomView.leftButton.enabled = false;
        self.bottomView.rightButton.enabled = false;
        [self.codeView showContent:false];
        [self.codeView bc_showLoading];
    }
    UserWalletChargeRequest *request = [UserWalletChargeRequest new];
    request.cur_id = currId;
    WEAK_SELF
    [request didRequestCompleted:^{
        if (init) {
            [wself.view bc_hiddenLoading];
        }
        if (change) {
            [wself.codeView bc_hiddenError];
            [wself.codeView bc_hiddenLoading];
        }
    }];
    [request didRequestSuccessed:^(id result, id page) {
        UserWalletChargeResponse *resp = result;
        [wself handleData:resp.data init:init change:change];
    }];
    [request didRequestFailed:^(NSError *error) {
        if (error&&wself.view) {
            NSString *errorMsg = error.domain;
            [QMUITips showError:errorMsg inView:wself.view hideAfterDelay:[QMUITips smartDelaySecondsForTipsText:errorMsg]];
            if (init) {
                [wself.view bc_showWithInsets:UIEdgeInsetsMake(CGRectGetHeight(wself.customNaviBar.frame), 0, 0, 0) error:error needRefresh:true refreshTitle:nil target:self refreshAction:@selector(requestInitFailedAction)];
            }
            if (change) {
                [wself.codeView bc_showError:error needRefresh:true
                                     refreshTitle:nil target:self refreshAction:@selector(requestContentScrollFialedAction)];
            }
        }
    }];
    [request requestServer];
}
- (void)requestInitFailedAction {
    [self.view bc_hiddenError];
    [self requestChargeCurrId:nil init:true change:false];
}
- (void)requestContentScrollFialedAction {
    [self.codeView bc_hiddenError];
    NSString *currId = [NSString stringWithFormat:@"%@",@(self.coinTypeButton.currData.cur_id)];
    [self requestChargeCurrId:currId init:false change:true];
}

- (void)handleData:(UserWalletChargeData *)chargeData init:(BOOL)init change:(BOOL)change {
    if (chargeData) {
        if (init) {
            self.coinTypeButton.alpha = 1;
            self.bottomView.alpha = 1;
            self.codeView.alpha = 1;
        }
        if (change) {
            [self.codeView showContent:true];
        }
        //设置数据
        self.title = chargeData.flow.title;
        //
        self.coinTypeButton.caption = chargeData.flow.select;
        self.coinTypeButton.coinType = chargeData.curr;
        //
        self.codeView.chargeData = chargeData;
        //
        [self.bottomView.leftButton.displayButton setTitle:chargeData.flow.qrCopy_submit forState:UIControlStateNormal];
        [self.bottomView.rightButton.displayButton setTitle:chargeData.flow.save_submit forState:UIControlStateNormal];
        //
        if (chargeData.address.length) {
            self.bottomView.leftButton.enabled = true;
            self.bottomView.rightButton.enabled = true;
        }else {
            self.bottomView.leftButton.enabled = false;
            self.bottomView.rightButton.enabled = false;
        }
    }
}


- (void)addressButtonDidClickedAction {
    NSString *pasteText = self.codeView.chargeData.address;
    if (pasteText) {
        UIPasteboard *pasteBoard = [UIPasteboard generalPasteboard];
        [pasteBoard setString:pasteText];
        if (self.view) {
            NSString *success = BCLocalizedKey(@"general_copy_success");
            [QMUITips showSucceed:success inView:self.view hideAfterDelay:[QMUITips smartDelaySecondsForTipsText:success]];
        }
    }
}
- (void)saveButtonDidClickedAction {
    UIImage *qrImage = self.codeView.qrImage;
    if (qrImage) {
        WEAK_SELF
        [[BCSavePhotoManager sharedSave] savePhotoToAlbum:qrImage complete:^(BOOL success, SavePhotoManagerError *error) {
            if (success) {
                if (wself.view) {
                    NSString *success = BCLocalizedKey(@"general_save_success");
                    [QMUITips showSucceed:success inView:self.view hideAfterDelay:[QMUITips smartDelaySecondsForTipsText:success]];
                }
            }
            if (error) {
                if (wself.view) {
                    NSString *errMsg = BCLocalizedKey(@"general_save_failure");
                    [QMUITips showError:errMsg inView:wself.view hideAfterDelay:[QMUITips smartDelaySecondsForTipsText:errMsg]];
                }
            }
        }];
    }
}

#pragma mark - BCCoinTypeQrCodeViewDelegate
- (void)pasteAddressAction:(NSString *)pasteText {
    [self addressButtonDidClickedAction];
}




//默认请求一次币种列表信息
- (void)requestCurrList {
    BCUserWalletCurrRequest *request = [BCUserWalletCurrRequest new];
    //
    WEAK_SELF
    [request didRequestSuccessed:^(id result, id page) {
        BCUserWalletCurrResponse *resp = result;
        wself.walletCurrData = resp.data;
    }];
    [request requestServer];
}

@end
